package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 插入订单数据
     * @param orders
     */
    void insert(Orders orders);


    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);


    /**
     * 订单分页查询
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);


    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders selectById(Long id);

    Page<Orders> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO);

    @Select("select count(id) from orders where status=#{status}")
    Integer countStatus(Integer status);

    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    /**
     * 查询超时订单
     * @param pendingPayment
     * @param timeoutTime
     * @return
     */
    @Select("select * from orders where status=#{pendingPayment} and order_time<#{timeoutTime}")
    List<Orders> getTimeOutOrders(Integer pendingPayment, LocalDateTime timeoutTime);

    /**
     * 查询超过凌晨一点上一个工作日派送中的订单
     * @param deliveryInProgress
     * @param time
     * @return
     */
    @Select("select * from orders where status= #{deliveryInProgress} and orders.order_time<#{time}")
    List<Orders> getDeliveryOrders(Integer deliveryInProgress, LocalDateTime time);

    /**
     * 统计每天的营业额
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 统计每天总或者有效营业单数
     * @param map
     * @return
     */
    Integer countByMap(Map map);

    /**
     * 统计指定时间区间内的销量排名前10
     * @param begintime
     * @param endtime
     * @return
     */
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begintime, LocalDateTime endtime);
}
